package com.cpecc.dao.wuzi;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.wuzi.CaiGou;
import com.cpecc.model.wuzi.HeTong;

@Repository
public class HeTongDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void save(HeTong heTong){
		Session session=sessionFactory.getCurrentSession();
		session.save(heTong);
		session.flush();
	}
	
	public boolean cover(HeTong heTong){
		String hql="update HeTong t set t.caiGou=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, heTong.getCaiGou());
		query.setString(1, heTong.getId());
		return query.executeUpdate()>0;
	}
	
	public HeTong get(String id){
		return (HeTong) sessionFactory.getCurrentSession().get(HeTong.class, id);
	}
	
	public boolean delete(String id){
		String hql="delete HeTong t where t.id=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);		
		return (query.executeUpdate() > 0);
	}
	
	@SuppressWarnings("unchecked")
	public List<HeTong> list(){
		String hql="from HeTong t where t.caiGou is not null";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<HeTong> list(String fileName){
		String hql="from HeTong t where t.ysWJM like ? and t.caiGou is not null";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, "%"+fileName+"%");
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<HeTong> list(CaiGou caiGou){
		String hql="from HeTong t where t.caiGou=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, caiGou);
		return query.list();
	}
}
